/**
 * 
 */
package co.gov.medellin.sdep.sisdep.operativa.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.operativa.exception.RecursoHumanoException;
import co.gov.medellin.sdep.sisdep.operativa.exception.RecursoHumanoRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.exception.descriptor.RecursoHumanoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.admin.exception.PersonaException;
import co.gov.medellin.sdep.sisdep.admin.exception.PersonaRuntimeException;
import co.gov.medellin.sdep.sisdep.admin.exception.descriptor.PersonaMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Persona;
import co.gov.medellin.sdep.sisdep.model.RecursoHumano;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IRecursoHumanoDAOT;

/**
 * @author Usuario
 *
 */
public class RecursoHumanoServiceImpl extends BaseService implements IRecursoHumanoService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IRecursoHumanoDAOT recursoHumanoDAO;

	/**
	 * 
	 */
	public RecursoHumanoServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(RecursoHumano recursoHumano) throws RecursoHumanoException {
		try{
			recursoHumano.setUsuarioCrea(getPrincipalUser());
			recursoHumano.setFechaCrea(new Date());
			getRecursoHumanoDAO().insert(recursoHumano);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new RecursoHumanoException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new RecursoHumanoException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(RecursoHumano recursoHumano) throws RecursoHumanoException {
		try{
			recursoHumano.setUsuarioModifica(getPrincipalUser());
			recursoHumano.setFechaModifica(new Date());
			getRecursoHumanoDAO().update(recursoHumano);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new RecursoHumanoRuntimeException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new RecursoHumanoRuntimeException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(RecursoHumano recursoHumano) throws RecursoHumanoException {
		try {
			recursoHumano.setUsuarioModifica(getPrincipalUser());
			recursoHumano.setFechaModifica(new Date());
			getRecursoHumanoDAO().delete(recursoHumano);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new RecursoHumanoRuntimeException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, RecursoHumano recursoHumano) throws RecursoHumanoException {
		try{
			//return getRecursoHumanoDAO().getAllRecursoHumanoPaginated(start, max, criteria);
			return getRecursoHumanoDAO().load(recursoHumano);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new RecursoHumanoRuntimeException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final RecursoHumano get(long id) throws RecursoHumanoException {
		try{
			return getRecursoHumanoDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new RecursoHumanoRuntimeException(new RecursoHumanoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}
	
	/**
	 * 
	 */
	public final RecursoHumano findByRelated(RecursoHumano recursoHumano) throws RecursoHumanoException {
		try{
			return getRecursoHumanoDAO().findByCriteria(recursoHumano);
		}catch (Exception e) {
			Object [] infoError = {new Long(0)};
			throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}
	

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IRecursoHumanoDAOT getRecursoHumanoDAO() {
		return recursoHumanoDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setRecursoHumanoDAO(IRecursoHumanoDAOT recursoHumanoDAO) {
		this.recursoHumanoDAO = recursoHumanoDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
